#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_dckerr.info <<'%EOF%'
   geoopt_dckerr
   -------------
   Molecule:         O3
   Wave Function:    MCSCF (RAS) / aug-cc-pVDZ
   Test Purpose:     Geometry optimization using .OPTIMIZE module
                     (1st order) with a calculation of DC-Kerr effect
                     at the optimized geometry
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_dckerr.mol <<'%EOF%'
BASIS
aug-cc-pVDZ
Geometry optimization, calculation of DC-Kerr effect
at the optimized geometry
Atomtypes=1 Generators=2  Y  Z
Charge=8.0 Atoms=2
OS         .0000000000        0.0000000000         .0000000000             *
OP        1.2634502500        2.0449019100         .0000000000             *
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_dckerr.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
**WAVE FUNCTIONS
.HF
.MP2
.MCSCF
.NSYM
 4
*SCF INPUT
.DOUBLY OCCUPIED
 6 4 1 1
*CONFIGURATION INPUT
.SYMMETRY
 1
.SPIN MUL
 1
.INACTIVE
 4 2 0 0
.ELECTRONS
 12
.RAS1 SPACE
 2 2 1 0
.RAS1 HOLES
 0 2
.RAS2 SPACE
 0 0 1 1
.RAS3 SPACE
 1 1 0 0
.RAS3 ELECTRONS
 0 2
**RESPONSE
*CUBIC
.DC-KER
.FREQUE
 2
 0.0 0.0592
.APROP
ZDIPLEN
.BPROP
ZDIPLEN
.CPROP
ZDIPLEN
.DPROP
ZDIPLEN
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_dckerr.check
cat >>geoopt_dckerr.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# Molecular geometry
CRIT1=`$GREP "1 * x * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "2 * y * (0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP "3 * z * (0| )\.0000000000" $log | wc -l`
CRIT4=`$GREP "4 * x * 1\.2634502500" $log | wc -l`
CRIT5=`$GREP "5 * y * 2\.0449019100" $log | wc -l`
CRIT6=`$GREP "6 * z * (0| )\.0000000000" $log | wc -l`
CRIT7=`$GREP "7 * x * 1\.2634502500" $log | wc -l`
CRIT8=`$GREP "8 * y * \-2\.0449019100" $log | wc -l`
CRIT9=`$GREP "9 * z * (0| )\.0000000000" $log | wc -l`
CRIT10=`$GREP "Total number of coordinates\: * 9" $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10`
CTRL[1]=10
ERROR[1]="GEOMETRY NOT CORRECT"

# Symmetry
CRIT1=`$GREP "Number of coordinates in each symmetry\: * 3 * 3 * 2 * 1" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry\: * 27 * 21 * 12 * 9" $log | wc -l`
TEST[2]=`expr	$CRIT1 \+ $CRIT2`
CTRL[2]=2
ERROR[2]="SYMMETRY NOT CORRECT"

# MCSCF energy
CRIT1=`$GREP "\= MP2 second order energy *\: * \-224\.963913" $log | wc -l`
CRIT2=`$GREP "Final MCSCF energy\: * \-224\.510748516" $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2`
CTRL[3]=2
ERROR[3]="INITIAL MCSCF ENERGY NOT CORRECT"

# Energy
CRIT1=`$GREP "Total energy * \-224\.510748516[0-9] au \(Hartrees\)" $log | wc -l`
CRIT2=`$GREP "\-6109\.2482371[0-9] eV" $log | wc -l`
CRIT3=`$GREP "\-589452\.879[0-9] kJ\/mol" $log | wc -l`
TEST[5]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[5]=3
ERROR[5]="INITIAL ENERGY NOT CORRECT"

# Gradient
CRIT1=`$GREP "OS * x * (0| )\.00130" $log | wc -l`
CRIT2=`$GREP "OP * x * (\-0|\-)\.00130" $log | wc -l`
CRIT3=`$GREP "OP * y * (\-0|\-)\.00188" $log | wc -l`
TEST[6]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[6]=3
ERROR[6]="INITIAL GRADIENT NOT CORRECT"

# Dipole moment
CRIT1=`$GREP "(0| )\.20476. * (0| )\.5204(4|5). * 1\.7360" $log | wc -l`
CRIT2=`$GREP "x * (\-0|\-)\.20476... * (\-0|\-)\.52045... * \-1\.7360" $log | wc -l`
TEST[7]=`expr	$CRIT1 \+ $CRIT2`
CTRL[7]=2
ERROR[7]="INITIAL DIPOLE MOMENT NOT CORRECT"

# Step
CRIT1=`$GREP "OS * \-0*\.00068....[0-9] * (0| )\.0000000000 * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "OP *_1 * 1\.2637.....[0-9] * 2\.045......[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP "OP *_2 * 1\.2637.....[0-9] * \-2\.045......[0-9] * (0| )\.0000000000" $log | wc -l`
TEST[8]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[8]=3
ERROR[8]="FIRST GEOMETRY STEP NOT CORRECT"

# Final geometry
CRIT1=`$GREP -l "OS *  \-0*\.00140....[0-9] * (0| )\.0000000000 * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP -l "OP *_1 * 1\.26415....[0-9] *   2\.04633....[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP -l "OP *_2 * 1\.26415....[0-9] * \-2\.04633....[0-9] * (0| )\.0000000000" $log | wc -l`
TEST[11]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[11]=3
ERROR[11]="FINAL GEOMETRY NOT CORRECT"

# Gamma values
CRIT1=`$GREP -l "\@ *gamma\(Z\;Z\,Z\,Z\) * 207\.46" $log | wc -l`
CRIT2=`$GREP -l "\@ *gamma\(Z\;Z\,Z\,Z\) * 214\.81" $log | wc -l`
TEST[12]=`expr	$CRIT1 \+ $CRIT2`
CTRL[12]=2
ERROR[12]="GAMMA VALUES NOT CORRECT"

# Geometry optimization
CRIT2=`$GREP "Energy at final geometry is * \: * \-224\.510751 a\.u\." $log | wc -l`
CRIT3=`$GREP "Energy change during optimization \: * (\-0|\-)\.000003 a\.u\." $log | wc -l`
TEST[13]=`expr	$CRIT2 \+ $CRIT3`
CTRL[13]=2
ERROR[13]="GEOMETRY OPTIMIZATION NOT CORRECT"

PASSED=1
for i in 1 2 3 5 6 7 8 11 12 13
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} : ${TEST[i]} .ne. ${CTRL[i]} ; "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
